// StudentDTO.java
package com.university.mentalhealthplatform.teacherV2.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.university.mentalhealthplatform.appModels.Student;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;

@Getter
@Setter
public class StudentDTO extends UserDTO {
    private UUID id;
    private String username; // 添加用户名字段
    private String userType = "STUDENT";
    private String address;
    private String password; // 添加密码字段

    @JsonFormat(pattern = "yyyy-MM-dd")
    private LocalDate birthDate;

    private String name;
    private String number;
    private String gender;
    private String major;
    private String grade;
    private String college;

    public Student convertToEntity() {
        Student student = new Student();
        student.setId(this.id);
        student.setUsername(this.username != null ? this.username : this.number); // 设置默认用户名为学号
        student.setUserType(this.userType);
        student.setAddress(this.address);
        student.setPassword(this.password != null ? this.password : "defaultPassword"); // 设置默认密码
        student.setBirthDate(Date.from(this.birthDate.atStartOfDay(ZoneId.systemDefault()).toInstant()));
        student.setName(this.name);
        student.setNumber(this.number);
        student.setGender(this.gender);
        student.setMajor(this.major);
        student.setGrade(this.grade);
        student.setCollege(this.college);
        return student;
    }

    public static StudentDTO convertToDTO(Student student) {
        StudentDTO studentDTO = new StudentDTO();
        studentDTO.setId(student.getId());
        studentDTO.setUsername(student.getUsername());
        studentDTO.setUserType(student.getUserType());
        studentDTO.setAddress(student.getAddress());
        studentDTO.setPassword(student.getPassword());
        if (student.getBirthDate() != null) {
            studentDTO.setBirthDate(student.getBirthDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDate());
        }
        studentDTO.setName(student.getName());
        studentDTO.setNumber(student.getNumber());
        studentDTO.setGender(student.getGender());
        studentDTO.setMajor(student.getMajor());
        studentDTO.setGrade(student.getGrade());
        studentDTO.setCollege(student.getCollege());
        return studentDTO;
    }
}